# 构建阶段
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

# 复制项目文件
COPY ["src/FServer.API/FServer.API.csproj", "src/FServer.API/"]
COPY ["src/FServer.Core/FServer.Core.csproj", "src/FServer.Core/"]
COPY ["src/FServer.Infrastructure/FServer.Infrastructure.csproj", "src/FServer.Infrastructure/"]
COPY ["src/FServer.Application/FServer.Application.csproj", "src/FServer.Application/"]

# 恢复依赖
RUN dotnet restore "src/FServer.API/FServer.API.csproj"

# 复制所有源代码
COPY . .

# 生成 Protocol Buffers 代码
RUN dotnet build "src/FServer.API/FServer.API.csproj" -c Release -o /app/build

# 发布阶段
FROM build AS publish
RUN dotnet publish "src/FServer.API/FServer.API.csproj" -c Release -o /app/publish /p:UseAppHost=false

# 运行阶段
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
WORKDIR /app

# 安装必要的系统包
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/*

# 复制发布文件
COPY --from=publish /app/publish .

# 创建日志目录
RUN mkdir -p /app/logs

# 设置环境变量
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:80

# 暴露端口
EXPOSE 80 443 8080 8081 8082 8083

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:80/health || exit 1

# 启动应用
ENTRYPOINT ["dotnet", "FServer.API.dll"]
